package impl

import (
	"context"
	"log"
	"net/http"

	"github.com/cloudwego/hertz/pkg/app"
	"github.com/cloudwego/kitex/client"
	"hertz-nbdemo/biz/service"
	"hertz-nbdemo/kitex_gen/volo/example"
	"hertz-nbdemo/kitex_gen/volo/example/itemservice"
	"hertz-nbdemo/pkg/logger"
	"hertz-nbdemo/util/common"
)

var _ service.VoloService = &VoloServiceImpl{}

type VoloServiceImpl struct {
	BaseService
	VoloClient itemservice.Client
}

func NewVoloService() service.VoloService {
	c, err := itemservice.NewClient("volo.example", client.WithHostPorts("0.0.0.0:7001"))
	if err != nil {
		log.Fatal(err)
	}
	return &VoloServiceImpl{
		VoloClient: c,
	}
}

func (v *VoloServiceImpl) Volo(ctx context.Context, c *app.RequestContext) {
	r, err := v.VoloClient.GetItem(ctx, &example.GetItemRequest{Id: 123456})
	if err != nil {
		logger.Logger.Error(err.Error())
	}
	c.JSON(http.StatusOK, common.Success(*r))
}
